﻿using HDW_Migrate.Extensions;

namespace HDW_Migrate.Resources;

/// <summary>
/// 系统配置
/// </summary>
public class SystemSettingDto
{
    /// <summary>
    /// 区域中心数据库连接字符串（如：Password=123456;Persist Security Info=True;User ID=sa;Initial Catalog=HINAMIIS_Domain;Data Source=192.168.1.1\sql;MultipleActiveResultSets=True）
    /// </summary>
    public string SourceDbConnection { get; set; }
    /// <summary>
    /// RIS 服务地址（如：http://192.168.1.1:10081）
    /// </summary>
    public string RisUrl { get; set; }

    /// <summary>
    /// 保存连接配置
    /// </summary>
    public void Save()
    {
        var dir = Path.Combine(Environment.CurrentDirectory, "setting_directory");
        Directory.CreateDirectory(dir);
        var filepath = Path.Combine(dir, "SystemSetting.json");
        File.WriteAllText(filepath, this.ToJson());
    }

    /// <summary>
    /// 读取连接配置
    /// </summary>
    /// <returns></returns>
    public static SystemSettingDto Read()
    {
        var filepath = Path.Combine(Environment.CurrentDirectory, "setting_directory", "SystemSetting.json");
        if (!File.Exists(filepath)) return null;
        var text = File.ReadAllText(filepath);
        return text.ParseObject<SystemSettingDto>();
    }
}
